package com.qingfeng.qfcms.controller.Admin.role;

import com.qingfeng.qfcms.controller.BaseController;
import com.qingfeng.qfcms.domin.Role;
import com.qingfeng.qfcms.page.Page;
import com.qingfeng.qfcms.service.RoleService;
import com.qingfeng.qfcms.utils.PageData;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * ============================================================
 * 版权 ：青峰软件有限公司  版权所有 (c) 2016
 * 作者:  menxd
 * 版本 ：1.0
 * 创建日期 ： 2016/8/3
 * 描述 ：
 *    角色名称
 * 修订历史 ：
 *
 * ============================================================
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {


    @Resource
    private RoleService roleService;
    @RequestMapping("lst.shtml")
    public String lst(Page<Role > page, Model model, String ROLE_NAME){

        Map<String,Object> map=new HashedMap();
        if(null != ROLE_NAME && !"".equals(ROLE_NAME)){
            ROLE_NAME = ROLE_NAME.trim();
            map.put("ROLE_NAME", ROLE_NAME);
        }
        page.setParams(map);
        List<Role> roles=roleService.findPage(page);
        model.addAttribute("roles",roles);
        model.addAttribute("page",page.getPageStr("/role/lst.shtml"));
        return "/admin/frame/role/role_list";
    }

    @RequestMapping("toAddR.shtml")
    public String toAddR(){
        return "/admin/frame/role/role_add";
    }



    @RequestMapping("toEditR.shtml")
    public String toEditR(String user_id,Model model){
        Role role=roleService.get(user_id);
        model.addAttribute("role",role);
        return "/admin/frame/role/role_edit";
    }

    @RequestMapping("addR")
    @ResponseBody
   public boolean addR(Role role){
        try {

            roleService.insert(role);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

   }

   @RequestMapping("deleteR.shtml")
   @ResponseBody
   public boolean deleteR(String ROLE_ID){

     try {
         roleService.deleteById(ROLE_ID);
         return true;
     }catch (Exception e){
         e.printStackTrace();
         return false;
     }
   }

    @RequestMapping("deletesR.shtml")
    @ResponseBody
    public boolean deletesR(String[] ROLE_ID){

        try {
            roleService.delete(ROLE_ID);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("edit.shtml")
    @ResponseBody
    public boolean edit(Role role){
        try {
            roleService.update(role);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
